﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PCLawPrototype
{
    public partial class ManageLawyer : ManageRibbonForm
    {
        int iSelectedIndex = -1;
        List<Lawyer> _lawyers = new List<Lawyer>();

        public ManageLawyer()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;

            FillToGrid();
        }

        private void FillToGrid()
        {
            _lawyers = Common.Entity.Lawyers.ToList();
            lstClient.Items.Clear();

            foreach (Lawyer lawyer in _lawyers)
            {
                lawyer.LawyerRanks.Load();

                System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
                    lawyer.LawyerCode,
                    lawyer.LawyerName,
                    lawyer.CreatedDate.ToShortDateString(),
                    lawyer.LawyerRanks.First(lawy => lawy.Status == 1).RankValue,
                    lawyer.LawyerStatus == 1 ? "Open" : "Close"}, -1);
                this.lstClient.Items.Add(listViewItem1);

            }
        }

        private void lstClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstClient.SelectedIndices.Count > 0)
            {
                iSelectedIndex = lstClient.SelectedIndices[0];

                if (lstClient.Items[lstClient.SelectedIndices[0]].SubItems[4].Text == "Open")
                {
                    ((DStarter)this.MdiParent).ChangeActiveText("Deac&tive");
                }
                else
                {
                    ((DStarter)this.MdiParent).ChangeActiveText("Ac&tive");

                }
            }
            else
                iSelectedIndex = -1;
        }

        private void ManageLawyer_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
            ((DStarter)this.MdiParent).SetRPEventVisible(false);
        }
        #region Overrides of ManageRibbonForm

        public override void Add()
        {
            NewLawyerFrm lawyer = new NewLawyerFrm();
            lawyer.ShowDialog();

            Reload();
        }

        public override void Edit()
        {
            NewLawyerFrm lawyer;
            if (lstClient.SelectedIndices.Count > 0)
            {
                lawyer = new NewLawyerFrm(_lawyers[lstClient.SelectedIndices[0]]);
                lawyer.ShowDialog();

                Reload();
            }
        }

        public override void Delete()
        {
            if (lstClient.SelectedItems.Count != 0)
            {
                if (MessageBox.Show("Are you sure to delete?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Common.Entity.DeleteObject(_lawyers[lstClient.SelectedIndices[0]]);
                    lstClient.Items.Remove(lstClient.SelectedItems[0]);
                    Common.Entity.SaveChanges();
                }
            }
            else
            {
                MessageBox.Show("Select at least 1 lawyer to delete!");
            }
        }

        public override void Reload()
        {
            //MessageBox.Show("Reload data from database and fill data to list!", "Description for function", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Common.Entity.Refresh(System.Data.Objects.RefreshMode.ClientWins, Common.Entity.Lawyers);
            FillToGrid();
        }

        public override void Active()
        {
            if (((DStarter)this.MdiParent).GetActiveText() == "Ac&tive")
            {
                lstClient.Items[iSelectedIndex].SubItems[4].Text = "Open";
                lstClient.Items[iSelectedIndex].BackColor = Color.White;
                _lawyers[iSelectedIndex].LawyerStatus = 1;
            }
            else
            {
                lstClient.Items[iSelectedIndex].SubItems[4].Text = "Close";
                lstClient.Items[iSelectedIndex].BackColor = Color.FromArgb(80, 150, 150, 150);
                _lawyers[iSelectedIndex].LawyerStatus = 0;
            }

            Common.Entity.SaveChanges();
        }

        public override void StartSearch()
        {
            MessageBox.Show("Data filtered!");
        }

        #endregion

        private void ManageLawyer_Activated(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetRPGLawyerVisible(true);
            ((DStarter)this.MdiParent).SetEventFocus();
        }

        private void ManageLawyer_Deactivate(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetRPGLawyerVisible(false);
        }

        private void lstClient_DoubleClick(object sender, EventArgs e)
        {
            NewLawyerFrm lawyer;
            if (lstClient.SelectedIndices.Count > 0)
            {
                lawyer = new NewLawyerFrm(_lawyers[lstClient.SelectedIndices[0]]);
                lawyer.ShowDialog();

                Reload();
            }
        }

        private void lstClient_Click(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetEventFocus();
        }
    }
}
